package com.loongtech.bi.manager.realTime;

import com.loongtech.bi.action.PageHelper;
import com.loongtech.bi.action.UIHelper;
import com.loongtech.bi.entity.realTime.*;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.bi.manager.system.ConfigManager;
import com.loongtech.bi.support.chart.ChartData1;
import com.loongtech.bi.support.chart.ChartFlow;
import com.loongtech.bi.support.chart.ChartSelect;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.Map.Entry;

/**
 * 实时数据-分渠道概况manager
 *
 * @author marin
 * @date 2015-09-15
 */
@Service
public class PlatformAndServerRealTimeManager extends ManagerQueryCacheBase<Object> {
    private static final long serialVersionUID = 1L;

    @Autowired
    NewUserByDayManager newUserByDayManager;
    @Autowired
    DauByDayManager dauByDayManager;
    @Autowired
    AddCashUserByDayManager addCashUserByDayManager;
    @Autowired
    CashAddByDayManager cashAddByDayManager;
    @Autowired
    UserSecondLeftByDayManager userSecondLeftByDayManager;
    @Autowired
    UserThirdLeftByDayManager userThirdLeftByDayManager;
    @Autowired
    UserSevenLeftByDayManager userSevenLeftByDayManager;
    @Autowired
    NewUserByHourManager newUserByHourManager;
    @Autowired
    DauByHourManager dauByHourManager;
    @Autowired
    AddCashUserByHourManager addCashUserByHourManager;
    @Autowired
    CashAddByHourManager cashAddByHourManager;
    @Autowired
    NewUserAddCashUserByHourManager newUserAddCashUserByHourManager;
    @Autowired
    NewUserCashAddByHourManager newUserCashAddByHourManager;
    @Autowired
    ActiveDeviceByDayManager activeDeviceByDayManager;
    @Autowired
    OnlineByQuantumManager onlineByQuantumManager;
    @Autowired
    ConfigManager configManager;
    @Autowired
    NewDeviceByDayManager newDeviceByDayManager;


    public void updateDatas(Date lookDate, List<PlatformData> platformDatas, PlatformData allPlatformData, List<ChannelData> channelDatas) {
        Map<String, PlatformData> platformDataMap = new LinkedHashMap<>();
        Map<String, ChannelData> channelDataMap = new LinkedHashMap<>();

        // 平台设备激活
        List<Object[]> tmp00 = activeDeviceByDayManager.getPaltformActiveDeviceRealTimeDatas(lookDate);
        if (tmp00 != null && tmp00.size() > 0 && tmp00.get(0) != null) {
            for (Object[] e : tmp00) {
                String platformid = e[0].toString();
                int activeDevice = Integer.parseInt(e[1].toString());

                if ("0".equals(platformid)) {
                    allPlatformData.setPlatformid(platformid);
                    allPlatformData.setActivate(activeDevice);
                    allPlatformData.setNotempty(true);
                } else {
                    PlatformData p = platformDataMap.get(platformid);
                    if (p == null) {
                        p = new PlatformData();
                        p.setPlatformid(platformid);
                        platformDataMap.put(platformid, p);
                    }
                    p.setActivate(activeDevice);
                }
            }
        }


        // 渠道设备激活
        List<Object[]> tmp02 = activeDeviceByDayManager.getChannelActiveDeviceRealTimeDatas(lookDate);
        if (tmp02 != null && tmp02.size() > 0 && tmp02.get(0) != null) {
            for (Object[] e : tmp02) {
                String channelid = e[0].toString();
                int activeDevice = Integer.parseInt(e[1].toString());

                ChannelData c = channelDataMap.get(channelid);
                if (c == null) {
                    c = new ChannelData();
                    c.setChannelid(channelid);
                    channelDataMap.put(channelid, c);
                }
                c.setActivate(activeDevice);
            }
        }

        List<Object[]> tmp111 = newDeviceByDayManager.getPaltformNewDeviceRealTimeDatas(lookDate);
        if (tmp111 != null && tmp111.size() > 0 && tmp111.get(0) != null) {
            for (Object[] e : tmp111) {
                String platformid = e[0].toString();
                int newdevice = Integer.parseInt(e[1].toString());

                if ("0".equals(platformid)) {
                    allPlatformData.setPlatformid(platformid);
                    allPlatformData.setNewDevice(newdevice);
                    allPlatformData.setNotempty(true);
                } else {
                    PlatformData p = platformDataMap.get(platformid);
                    if (p == null) {
                        p = new PlatformData();
                        p.setPlatformid(platformid);
                        platformDataMap.put(platformid, p);
                    }
                    p.setNewDevice(newdevice);
                }
            }
        }

        List<Object[]> tmp000 = newDeviceByDayManager.getChannelNewDeviceRealTimeDatas(lookDate);
        if (tmp000 != null && tmp000.size() > 0 && tmp000.get(0) != null) {
            for (Object[] e : tmp000) {
                String channelid = e[0].toString();
                int newDevice = Integer.parseInt(e[1].toString());

                ChannelData c = channelDataMap.get(channelid);
                if (c == null) {
                    c = new ChannelData();
                    c.setChannelid(channelid);
                    channelDataMap.put(channelid, c);
                }
                c.setNewDevice(newDevice);
            }
        }

        // 平台新增
        List<Object[]> tmp1 = newUserByDayManager.getPaltformNewUserRealTimeDatas(lookDate);
        if (tmp1 != null && tmp1.size() > 0 && tmp1.get(0) != null) {
            for (Object[] e : tmp1) {
                String platformid = e[0].toString();
                int newUser = Integer.parseInt(e[1].toString());

                if ("0".equals(platformid)) {
                    allPlatformData.setPlatformid(platformid);
                    allPlatformData.setNewUser(newUser);
                    allPlatformData.setNotempty(true);
                } else {
                    PlatformData p = platformDataMap.get(platformid);
                    if (p == null) {
                        p = new PlatformData();
                        p.setPlatformid(platformid);
                        platformDataMap.put(platformid, p);
                    }
                    p.setNewUser(newUser);
                }
            }
        }

        // 渠道新增
        List<Object[]> tmp2 = newUserByDayManager.getChannelNewUserRealTimeDatas(lookDate);
        if (tmp2 != null && tmp2.size() > 0 && tmp2.get(0) != null) {
            for (Object[] e : tmp2) {
                String channelid = e[0].toString();
                int newUser = Integer.parseInt(e[1].toString());

                ChannelData c = channelDataMap.get(channelid);
                if (c == null) {
                    c = new ChannelData();
                    c.setChannelid(channelid);
                    channelDataMap.put(channelid, c);
                }
                c.setNewUser(newUser);
            }
        }

        // 平台活跃
        List<Object[]> tmp3 = dauByDayManager.getPaltformDauRealTimeDatas(lookDate);
        if (tmp3 != null && tmp3.size() > 0 && tmp3.get(0) != null) {
            for (Object[] e : tmp3) {
                String platformid = e[0].toString();
                int dau = Integer.parseInt(e[1].toString());

                if ("0".equals(platformid)) {
                    allPlatformData.setPlatformid(platformid);
                    allPlatformData.setActiveUser(dau);
                    allPlatformData.setNotempty(true);
                } else {
                    PlatformData p = platformDataMap.get(platformid);
                    if (p == null) {
                        p = new PlatformData();
                        p.setPlatformid(platformid);
                        platformDataMap.put(platformid, p);
                    }
                    p.setActiveUser(dau);
                }
            }
        }

        // 渠道活跃
        List<Object[]> tmp4 = dauByDayManager.getChannelDauRealTimeDatas(lookDate);
        if (tmp4 != null && tmp4.size() > 0 && tmp4.get(0) != null) {
            for (Object[] e : tmp4) {
                String channelid = e[0].toString();
                int dau = Integer.parseInt(e[1].toString());

                ChannelData c = channelDataMap.get(channelid);
                if (c == null) {
                    c = new ChannelData();
                    c.setChannelid(channelid);
                    channelDataMap.put(channelid, c);
                }
                c.setActiveUser(dau);
            }
        }

        // 平台付费帐号数
        List<Object[]> tmp5 = addCashUserByDayManager.getPaltformPayUserRealTimeDatas(lookDate);
        if (tmp5 != null && tmp5.size() > 0 && tmp5.get(0) != null) {
            for (Object[] e : tmp5) {
                String platformid = e[0].toString();
                int payUser = Integer.parseInt(e[1].toString());

                if ("0".equals(platformid)) {
                    allPlatformData.setPlatformid(platformid);
                    allPlatformData.setPayUser(payUser);
                    allPlatformData.setNotempty(true);
                } else {
                    PlatformData p = platformDataMap.get(platformid);
                    if (p == null) {
                        p = new PlatformData();
                        p.setPlatformid(platformid);
                        platformDataMap.put(platformid, p);
                    }
                    p.setPayUser(payUser);
                }
            }
        }

        // 渠道付费帐号数
        List<Object[]> tmp6 = addCashUserByDayManager.getChannelPayUserRealTimeDatas(lookDate);
        if (tmp6 != null && tmp6.size() > 0 && tmp6.get(0) != null) {
            for (Object[] e : tmp6) {
                String channelid = e[0].toString();
                int payUser = Integer.parseInt(e[1].toString());

                ChannelData c = channelDataMap.get(channelid);
                if (c == null) {
                    c = new ChannelData();
                    c.setChannelid(channelid);
                    channelDataMap.put(channelid, c);
                }
                c.setPayUser(payUser);
            }
        }

        // 平台付费金额
        List<Object[]> tmp7 = cashAddByDayManager.getPaltformPayRealTimeDatas(lookDate);
        if (tmp7 != null && tmp7.size() > 0 && tmp7.get(0) != null) {
            for (Object[] e : tmp7) {
                String platformid = e[0].toString();
                int pay = Integer.parseInt(e[1].toString());

                if ("0".equals(platformid)) {
                    allPlatformData.setPlatformid(platformid);
                    allPlatformData.setPay(UIHelper.getYuan(pay));
                    allPlatformData.setNotempty(true);
                } else {
                    PlatformData p = platformDataMap.get(platformid);
                    if (p == null) {
                        p = new PlatformData();
                        p.setPlatformid(platformid);
                        platformDataMap.put(platformid, p);
                    }
                    p.setPay(UIHelper.getYuan(pay));
                }
            }
        }

        // 渠道付费金额
        List<Object[]> tmp8 = cashAddByDayManager.getChannelPayRealTimeDatas(lookDate);
        if (tmp8 != null && tmp8.size() > 0 && tmp8.get(0) != null) {
            for (Object[] e : tmp8) {
                String channelid = e[0].toString();
                int pay = Integer.parseInt(e[1].toString());

                ChannelData c = channelDataMap.get(channelid);
                if (c == null) {
                    c = new ChannelData();
                    c.setChannelid(channelid);
                    channelDataMap.put(channelid, c);
                }
                c.setPay(UIHelper.getYuan(pay));
            }
        }

        // 平台活跃付费率,付费ARPU,活跃ARPU
        allPlatformData.setPayUserInActiveRadio(allPlatformData.getActiveUser() == 0 ? 0 : (float) (allPlatformData.getPayUser() * 1.0 / allPlatformData.getActiveUser()));
        allPlatformData.setPayARPU(allPlatformData.getPayUser() == 0 ? 0 : PageHelper.floatKeepTwoNumber(allPlatformData.getPay() * 1.0 / allPlatformData.getPayUser()));
        allPlatformData.setActiveARPU(allPlatformData.getActiveUser() == 0 ? 0 : PageHelper.floatKeepTwoNumber(allPlatformData.getPay() * 1.0 / allPlatformData.getActiveUser()));
        for (Entry<String, PlatformData> entry : platformDataMap.entrySet()) {
            PlatformData p = entry.getValue();
            p.setPayUserInActiveRadio(p.getActiveUser() == 0 ? 0 : (float) (p.getPayUser() * 1.0 / p.getActiveUser()));
            p.setPayARPU(p.getPayUser() == 0 ? 0 : PageHelper.floatKeepTwoNumber(p.getPay() * 1.0 / p.getPayUser()));
            p.setActiveARPU(p.getActiveUser() == 0 ? 0 : PageHelper.floatKeepTwoNumber(p.getPay() * 1.0 / p.getActiveUser()));
        }

        // 渠道活跃付费率,付费ARPU,活跃ARPU
        for (Entry<String, ChannelData> entry : channelDataMap.entrySet()) {
            ChannelData p = entry.getValue();
            p.setPayUserInActiveRadio(p.getActiveUser() == 0 ? 0 : (float) (p.getPayUser() * 1.0 / p.getActiveUser()));
            p.setPayARPU(p.getPayUser() == 0 ? 0 : PageHelper.floatKeepTwoNumber(p.getPay() * 1.0 / p.getPayUser()));
            p.setActiveARPU(p.getActiveUser() == 0 ? 0 : PageHelper.floatKeepTwoNumber(p.getPay() * 1.0 / p.getActiveUser()));
        }

        // 平台用户次留
        Date d2 = new Date(lookDate.getTime() - Time.DAY);
        List<Object[]> tmp9 = userSecondLeftByDayManager.getPaltform2LeftRealTimeDatas(d2);
        if (tmp9 != null && tmp9.size() > 0 && tmp9.get(0) != null) {
            for (Object[] e : tmp9) {
                String platformid = e[0].toString();
                float the2Left = Float.parseFloat(e[1].toString());

                if ("0".equals(platformid)) {
                    allPlatformData.setPlatformid(platformid);
                    allPlatformData.setThe2LeftRadio(the2Left);
                    allPlatformData.setNotempty(true);
                } else {
                    PlatformData p = platformDataMap.get(platformid);
                    if (p == null) {
                        p = new PlatformData();
                        p.setPlatformid(platformid);
                        platformDataMap.put(platformid, p);
                    }
                    p.setThe2LeftRadio(the2Left);
                }
            }
        }

        // 平台设备次留
        List<Object[]> mac9 = userSecondLeftByDayManager.getPaltform2LeftRealTimeMacDatas(d2);
        if (mac9 != null && mac9.size() > 0 && mac9.get(0) != null) {
            for (Object[] e : mac9) {
                String platformid = e[0].toString();
                float the2Left = Float.parseFloat(e[1].toString());

                if ("0".equals(platformid)) {
                    allPlatformData.setPlatformid(platformid);
                    allPlatformData.setThe2MacLeftRadio(the2Left);
                    allPlatformData.setNotempty(true);
                } else {
                    PlatformData p = platformDataMap.get(platformid);
                    if (p == null) {
                        p = new PlatformData();
                        p.setPlatformid(platformid);
                        platformDataMap.put(platformid, p);
                    }
                    p.setThe2MacLeftRadio(the2Left);
                }
            }
        }

        // 渠道用户次留
        List<Object[]> tmp10 = userSecondLeftByDayManager.getChannel2LeftRealTimeDatas(d2);
        if (tmp10 != null && tmp10.size() > 0 && tmp10.get(0) != null) {
            for (Object[] e : tmp10) {
                String channelid = e[0].toString();
                float the2Left = Float.parseFloat(e[1].toString());

                ChannelData c = channelDataMap.get(channelid);
                if (c == null) {
                    c = new ChannelData();
                    c.setChannelid(channelid);
                    channelDataMap.put(channelid, c);
                }
                c.setThe2LeftRadio(the2Left);
            }
        }

        // 渠道设备次留
        List<Object[]> mac10 = userSecondLeftByDayManager.getChannel2LeftRealTimeMacDatas(d2);
        if (mac10 != null && mac10.size() > 0 && mac10.get(0) != null) {
            for (Object[] e : mac10) {
                String channelid = e[0].toString();
                float the2Left = Float.parseFloat(e[1].toString());

                ChannelData c = channelDataMap.get(channelid);
                if (c == null) {
                    c = new ChannelData();
                    c.setChannelid(channelid);
                    channelDataMap.put(channelid, c);
                }
                c.setThe2MacLeftRadio(the2Left);
            }
        }

        Date d3 = new Date(lookDate.getTime() - 2 * Time.DAY);
        // 平台用户3留
        List<Object[]> tmp11 = userThirdLeftByDayManager.getPaltform3LeftRealTimeDatas(d3);
        if (tmp11 != null && tmp11.size() > 0 && tmp11.get(0) != null) {
            for (Object[] e : tmp11) {
                String platformid = e[0].toString();
                float the3Left = Float.parseFloat(e[1].toString());

                if ("0".equals(platformid)) {
                    allPlatformData.setPlatformid(platformid);
                    allPlatformData.setThe3LeftRadio(the3Left);
                    allPlatformData.setNotempty(true);
                } else {
                    PlatformData p = platformDataMap.get(platformid);
                    if (p == null) {
                        p = new PlatformData();
                        p.setPlatformid(platformid);
                        platformDataMap.put(platformid, p);
                    }
                    p.setThe3LeftRadio(the3Left);
                }
            }
        }

        // 平台设备3留
        List<Object[]> mac11 = userThirdLeftByDayManager.getPaltform3LeftRealTimeMacDatas(d3);
        if (mac11 != null && mac11.size() > 0 && mac11.get(0) != null) {
            for (Object[] e : mac11) {
                String platformid = e[0].toString();
                float the3Left = Float.parseFloat(e[1].toString());

                if ("0".equals(platformid)) {
                    allPlatformData.setPlatformid(platformid);
                    allPlatformData.setThe3MacLeftRadio(the3Left);
                    allPlatformData.setNotempty(true);
                } else {
                    PlatformData p = platformDataMap.get(platformid);
                    if (p == null) {
                        p = new PlatformData();
                        p.setPlatformid(platformid);
                        platformDataMap.put(platformid, p);
                    }
                    p.setThe3MacLeftRadio(the3Left);
                }
            }
        }

        // 渠道用户3留
        List<Object[]> tmp12 = userThirdLeftByDayManager.getChannel3LeftRealTimeDatas(d3);
        if (tmp12 != null && tmp12.size() > 0 && tmp12.get(0) != null) {
            for (Object[] e : tmp12) {
                String channelid = e[0].toString();
                float the3Left = Float.parseFloat(e[1].toString());

                ChannelData c = channelDataMap.get(channelid);
                if (c == null) {
                    c = new ChannelData();
                    c.setChannelid(channelid);
                    channelDataMap.put(channelid, c);
                }
                c.setThe3LeftRadio(the3Left);
            }
        }

        // 渠道设备3留
        List<Object[]> mac12 = userThirdLeftByDayManager.getChannel3LeftRealTimeMacDatas(d3);
        if (mac12 != null && mac12.size() > 0 && mac12.get(0) != null) {
            for (Object[] e : mac12) {
                String channelid = e[0].toString();
                float the3Left = Float.parseFloat(e[1].toString());

                ChannelData c = channelDataMap.get(channelid);
                if (c == null) {
                    c = new ChannelData();
                    c.setChannelid(channelid);
                    channelDataMap.put(channelid, c);
                }
                c.setThe3MacLeftRadio(the3Left);
            }
        }

        Date d7 = new Date(lookDate.getTime() - 6 * Time.DAY);
        // 平台用户7留
        List<Object[]> tmp13 = userSevenLeftByDayManager.getPaltform7LeftRealTimeDatas(d7);
        if (tmp13 != null && tmp13.size() > 0 && tmp13.get(0) != null) {
            for (Object[] e : tmp13) {
                String platformid = e[0].toString();
                float the7Left = Float.parseFloat(e[1].toString());

                if ("0".equals(platformid)) {
                    allPlatformData.setPlatformid(platformid);
                    allPlatformData.setThe7LeftRadio(the7Left);
                    allPlatformData.setNotempty(true);
                } else {
                    PlatformData p = platformDataMap.get(platformid);
                    if (p == null) {
                        p = new PlatformData();
                        platformDataMap.put(platformid, p);
                    }
                    p.setThe7LeftRadio(the7Left);
                }
            }
        }

        // 平台设备7留
        List<Object[]> mac13 = userSevenLeftByDayManager.getPaltform7LeftRealTimeMacDatas(d7);
        if (mac13 != null && mac13.size() > 0 && mac13.get(0) != null) {
            for (Object[] e : mac13) {
                String platformid = e[0].toString();
                float the7Left = Float.parseFloat(e[1].toString());

                if ("0".equals(platformid)) {
                    allPlatformData.setPlatformid(platformid);
                    allPlatformData.setThe7MacLeftRadio(the7Left);
                    allPlatformData.setNotempty(true);
                } else {
                    PlatformData p = platformDataMap.get(platformid);
                    if (p == null) {
                        p = new PlatformData();
                        platformDataMap.put(platformid, p);
                    }
                    p.setThe7MacLeftRadio(the7Left);
                }
            }
        }

        // 渠道用户7留
        List<Object[]> tmp14 = userSevenLeftByDayManager.getChannel7LeftRealTimeDatas(d7);
        if (tmp14 != null && tmp14.size() > 0 && tmp14.get(0) != null) {
            for (Object[] e : tmp14) {
                String channelid = e[0].toString();
                float the7Left = Float.parseFloat(e[1].toString());

                ChannelData c = channelDataMap.get(channelid);
                if (c == null) {
                    c = new ChannelData();
                    c.setChannelid(channelid);
                    channelDataMap.put(channelid, c);
                }
                c.setThe7LeftRadio(the7Left);
            }
        }

        // 渠道设备7留
        List<Object[]> mac14 = userSevenLeftByDayManager.getChannel7LeftRealTimeMacDatas(d7);
        if (mac14 != null && mac14.size() > 0 && mac14.get(0) != null) {
            for (Object[] e : mac14) {
                String channelid = e[0].toString();
                float the7Left = Float.parseFloat(e[1].toString());

                ChannelData c = channelDataMap.get(channelid);
                if (c == null) {
                    c = new ChannelData();
                    c.setChannelid(channelid);
                    channelDataMap.put(channelid, c);
                }
                c.setThe7MacLeftRadio(the7Left);
            }
        }

        platformDatas.addAll(new ArrayList<>(platformDataMap.values()));
        Collections.sort(platformDatas, new PlatformData().comparator);
        channelDatas.addAll(new ArrayList<>(channelDataMap.values()));
        for (ChannelData cdata : channelDatas) {
            if (cdata.getActivate() != 0) {
                float f = (float)cdata.getNewDevice()/cdata.getActivate();
                BigDecimal bg = new BigDecimal(f);
                cdata.setNewActRate(bg.setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
            } else {
                cdata.setNewActRate(0);
            }
        }
    }

    public List<ChannelOnlineRealTimeData> getChannelOnlineRealTimeDatas(Date lookDate, List<String> channelids) {
        Map<String, ChannelOnlineRealTimeData> result = new HashMap<>(); // channelid-minuteStr=ChannelOnlineRealTimeData

        // online
        List<EntityOnlineByQuantum> tmp1 = onlineByQuantumManager.getOnlineRealTimeDatasByChannels(lookDate, channelids);
        if (tmp1 != null) {
            String conf = configManager.getConfigValueByName(ConfigManager.ONLINE_RADIO);

            for (EntityOnlineByQuantum e : tmp1) {
                String key = e.getChannelid() + "-" + doQuantum(e.getQuantum(), conf);
                ChannelOnlineRealTimeData t = result.get(key);
                if (t == null) {
                    t = new ChannelOnlineRealTimeData();
                    t.setChannelid(e.getChannelid());
                    t.setMinuteStr(doQuantum(e.getQuantum(), conf));
                    result.put(key, t);
                }
                t.setOnline(e.getOnlinenum());
            }
        }

        // 处理在线两点数据不协调问题
        for (Entry<String, ChannelOnlineRealTimeData> entry : result.entrySet()) {
            ChannelOnlineRealTimeData t = entry.getValue();
            if ("00:00".equals(t.getMinuteStr())) {
                ChannelOnlineRealTimeData t1 = result.get(t.getChannelid() + "-00:05");
                if (t1 != null) {
                    t.setOnline(t1.getOnline());
                }
            }
            if ("23:59".equals(t.getMinuteStr())) {
                ChannelOnlineRealTimeData t1 = result.get(t.getChannelid() + "-23:55");
                if (t1 != null) {
                    t.setOnline(t1.getOnline());
                }
            }
        }

        List<ChannelOnlineRealTimeData> t = new ArrayList<>(result.values());
        Collections.sort(t, new ChannelOnlineRealTimeData().comparator);
        return t;
    }

    // 处理时间字符串，当在线日志不是5分钟记一次的话，需要对齐
    private String doQuantum(String quantum, String conf) {
        if (conf != null && !"5".equals(conf) && quantum.endsWith("5")) {
            return quantum.substring(0, quantum.length() - 1) + "0";
        } else {
            return quantum;
        }
    }

    public void updateChannelHourDatas(Date lookDate, String selectZhiBiaoItem, String selectedPlatform, List<ChannelHourData> channelHourDatas) {
        Map<String, ChannelHourData> tmp = new LinkedHashMap<>();

        switch (selectZhiBiaoItem) {
            case "newUser":
                List<EntityNewUserByHour> list = newUserByHourManager.getChannelHourNewUserByDate(lookDate, "0", "0", "0");
                for (EntityNewUserByHour l : list) {
                    String cid = l.getChannelid();

                    if (!"all".equals(selectedPlatform)) {
                        String pid = BiPlatformManager.CHANNEL_PLATFORM_MAP.get(cid);
                        if (pid == null || !pid.equals(selectedPlatform)) {
                            continue;
                        }
                    }

                    ChannelHourData t = tmp.get(cid);
                    if (t == null) {
                        t = new ChannelHourData();
                        t.setChannelid(cid);
                        t.setDatas(new LinkedHashMap<Integer, String>());

                        tmp.put(cid, t);
                    }
                    t.getDatas().put(l.getHour(), l.getNew_user() + "");
                }

                break;
            case "activeUser":
                List<EntityDauByHour> list1 = dauByHourManager.getChannelHourActiveUserByDate(lookDate, "0", "0", "0");
                for (EntityDauByHour l : list1) {
                    String cid = l.getChannelid();

                    if (!"all".equals(selectedPlatform)) {
                        String pid = BiPlatformManager.CHANNEL_PLATFORM_MAP.get(cid);
                        if (pid == null || !pid.equals(selectedPlatform)) {
                            continue;
                        }
                    }

                    ChannelHourData t = tmp.get(cid);
                    if (t == null) {
                        t = new ChannelHourData();
                        t.setChannelid(cid);
                        t.setDatas(new LinkedHashMap<Integer, String>());

                        tmp.put(cid, t);
                    }
                    t.getDatas().put(l.getHour(), l.getDau() + "");
                }

                break;
            case "payUser":
                List<EntityAddCashUserByHour> list2 = addCashUserByHourManager.getChannelHourPayUserByDate(lookDate, "0", "0", "0");
                for (EntityAddCashUserByHour l : list2) {
                    String cid = l.getChannelid();

                    if (!"all".equals(selectedPlatform)) {
                        String pid = BiPlatformManager.CHANNEL_PLATFORM_MAP.get(cid);
                        if (pid == null || !pid.equals(selectedPlatform)) {
                            continue;
                        }
                    }

                    ChannelHourData t = tmp.get(cid);
                    if (t == null) {
                        t = new ChannelHourData();
                        t.setChannelid(cid);
                        t.setDatas(new LinkedHashMap<Integer, String>());

                        tmp.put(cid, t);
                    }
                    t.getDatas().put(l.getHour(), l.getAddcash_user() + "");
                }

                break;
            case "pay":
                List<EntityCashAddByHour> list3 = cashAddByHourManager.getChannelHourCashAddByDate(lookDate, "0", "0", "0");
                for (EntityCashAddByHour l : list3) {
                    String cid = l.getChannelid();

                    if (!"all".equals(selectedPlatform)) {
                        String pid = BiPlatformManager.CHANNEL_PLATFORM_MAP.get(cid);
                        if (pid == null || !pid.equals(selectedPlatform)) {
                            continue;
                        }
                    }

                    ChannelHourData t = tmp.get(cid);
                    if (t == null) {
                        t = new ChannelHourData();
                        t.setChannelid(cid);
                        t.setDatas(new LinkedHashMap<Integer, String>());

                        tmp.put(cid, t);
                    }
                    t.getDatas().put(l.getHour(), UIHelper.getYuan(l.getCashadd()) + "");
                }

                break;
            case "newPayUser":
                List<EntityNewUserAddCashUserByHour> list4 = newUserAddCashUserByHourManager.getChannelHourNewUserAddCashUserByDate(lookDate, "0", "0", "0");
                for (EntityNewUserAddCashUserByHour l : list4) {
                    String cid = l.getChannelid();

                    if (!"all".equals(selectedPlatform)) {
                        String pid = BiPlatformManager.CHANNEL_PLATFORM_MAP.get(cid);
                        if (pid == null || !pid.equals(selectedPlatform)) {
                            continue;
                        }
                    }

                    ChannelHourData t = tmp.get(cid);
                    if (t == null) {
                        t = new ChannelHourData();
                        t.setChannelid(cid);
                        t.setDatas(new LinkedHashMap<Integer, String>());

                        tmp.put(cid, t);
                    }
                    t.getDatas().put(l.getHour(), l.getNew_user_addcash_user() + "");
                }

                break;
            case "newPay":
                List<EntityNewUserCashAddByHour> list5 = newUserCashAddByHourManager.getChannelHourNewUserCashAddByDate(lookDate, "0", "0", "0");
                for (EntityNewUserCashAddByHour l : list5) {
                    String cid = l.getChannelid();

                    if (!"all".equals(selectedPlatform)) {
                        String pid = BiPlatformManager.CHANNEL_PLATFORM_MAP.get(cid);
                        if (pid == null || !pid.equals(selectedPlatform)) {
                            continue;
                        }
                    }

                    ChannelHourData t = tmp.get(cid);
                    if (t == null) {
                        t = new ChannelHourData();
                        t.setChannelid(cid);
                        t.setDatas(new LinkedHashMap<Integer, String>());

                        tmp.put(cid, t);
                    }
                    t.getDatas().put(l.getHour(), UIHelper.getYuan(l.getNew_user_cashadd()) + "");
                }

                break;
            case "activePayRadio":
                Map<String, Map<Integer, Integer>> tmpMap = new LinkedHashMap<>();
                List<EntityDauByHour> list71 = dauByHourManager.getChannelHourActiveUserByDate(lookDate, "0", "0", "0");
                for (EntityDauByHour l : list71) {
                    String cid = l.getChannelid();

                    if (!"all".equals(selectedPlatform)) {
                        String pid = BiPlatformManager.CHANNEL_PLATFORM_MAP.get(cid);
                        if (pid == null || !pid.equals(selectedPlatform)) {
                            continue;
                        }
                    }

                    Map<Integer, Integer> t = tmpMap.get(cid);
                    if (t == null) {
                        t = new LinkedHashMap<>();
                        tmpMap.put(cid, t);
                    }
                    t.put(l.getHour(), l.getDau());
                }
                List<EntityAddCashUserByHour> list72 = addCashUserByHourManager.getChannelHourPayUserByDate(lookDate, "0", "0", "0");
                for (EntityAddCashUserByHour l : list72) {
                    String cid = l.getChannelid();

                    if (!"all".equals(selectedPlatform)) {
                        String pid = BiPlatformManager.CHANNEL_PLATFORM_MAP.get(cid);
                        if (pid == null || !pid.equals(selectedPlatform)) {
                            continue;
                        }
                    }

                    Map<Integer, Integer> t = tmpMap.get(cid);
                    if (t != null) {
                        Integer v = t.get(l.getHour());
                        if (v != null) {
                            ChannelHourData tt = tmp.get(cid);
                            if (tt == null) {
                                tt = new ChannelHourData();
                                tt.setChannelid(cid);
                                tt.setDatas(new LinkedHashMap<Integer, String>());

                                tmp.put(cid, tt);
                            }
                            tt.getDatas().put(l.getHour(), PageHelper.double2Percent(v == 0 ? 0 : l.getAddcash_user() * 1.0 / v) + "");
                        }
                    }
                }

                break;
            default:
                List<EntityNewUserByHour> list6 = newUserByHourManager.getChannelHourNewUserByDate(lookDate, "0", "0", "0");
                for (EntityNewUserByHour l : list6) {
                    String cid = l.getChannelid();

                    if (!"all".equals(selectedPlatform)) {
                        String pid = BiPlatformManager.CHANNEL_PLATFORM_MAP.get(cid);
                        if (pid == null || !pid.equals(selectedPlatform)) {
                            continue;
                        }
                    }

                    ChannelHourData t = tmp.get(cid);
                    if (t == null) {
                        t = new ChannelHourData();
                        t.setChannelid(cid);
                        t.setDatas(new LinkedHashMap<Integer, String>());

                        tmp.put(cid, t);
                    }
                    t.getDatas().put(l.getHour(), l.getNew_user() + "");
                }

                break;
        }

        if (tmp.size() > 0) {
            channelHourDatas.addAll(tmp.values());
            Collections.sort(channelHourDatas, new ChannelHourData().comparator);
        }
    }

    public class ChannelHourData {
        private String channelid; // 渠道ID
        private Map<Integer, String> datas; // hour=num

        public final Comparator<ChannelHourData> comparator = new Comparator<ChannelHourData>() {
            public int compare(ChannelHourData s1, ChannelHourData s2) {
                return BiPlatformManager.getChannelNameById(s1.getChannelid()).compareTo(BiPlatformManager.getChannelNameById(s2.getChannelid()));
            }
        };

        public Map<Integer, String> getDatas() {
            return datas;
        }

        public void setDatas(Map<Integer, String> datas) {
            this.datas = datas;
        }

        public String getChannelid() {
            return channelid;
        }

        public void setChannelid(String channelid) {
            this.channelid = channelid;
        }
    }

    // 服务器实时数据
    public class ChannelOnlineRealTimeData {
        @ChartFlow(name = "分钟")
        private String minuteStr; // 分钟，每五分钟1个，一天288个
        private String channelid; // 渠道ID
        private String channelName; // 渠道名称
        @ChartSelect(name = "在线", key = 1)
        @ChartData1(name = "在线", key = 1, multiByFiled = "channelName")
        private long online; // 在线

        // 比较器
        public final Comparator<ChannelOnlineRealTimeData> comparator = new Comparator<ChannelOnlineRealTimeData>() {
            public int compare(ChannelOnlineRealTimeData s1, ChannelOnlineRealTimeData s2) {
                return s1.getMinuteStr().compareTo(s2.getMinuteStr());
            }
        };

        public String getMinuteStr() {
            return minuteStr;
        }

        public void setMinuteStr(String minuteStr) {
            this.minuteStr = minuteStr;
        }

        public String getChannelid() {
            return channelid;
        }

        public void setChannelid(String channelid) {
            this.channelid = channelid;
            this.channelName = BiPlatformManager.getChannelNameById(channelid);
        }

        public String getChannelName() {
            return channelName;
        }

        public void setChannelName(String channelName) {
            this.channelName = channelName;
        }

        public long getOnline() {
            return online;
        }

        public void setOnline(long online) {
            this.online = online;
        }
    }

    // 平台数据
    public class PlatformData {
        private String platformid; // 平台ID
        private int activate;  // 激活数
        private int newDevice; // 新增设备
        private int newUser; // 新增帐号
        private int activeUser; // 活跃帐号
        private int payUser; // 付费帐号
        private int pay; // 付费金额
        private float payUserInActiveRadio; // 活跃付费率
        private float payARPU; // 付费ARPU
        private float activeARPU; // 活跃ARPU
        private float the2LeftRadio; // 账号次日留存率
        private float the3LeftRadio; // 账号3日留存率
        private float the7LeftRadio; // 账号7日留存率

        private float the2MacLeftRadio; // 设备次日留存率
        private float the3MacLeftRadio; // 设备3日留存率
        private float the7MacLeftRadio; // 设备7日留存率

        private boolean notempty; // 是否不空（查询时DB中无数据）

        public final Comparator<PlatformData> comparator = new Comparator<PlatformData>() {
            public int compare(PlatformData s1, PlatformData s2) {
                return s2.getNewUser() - s1.getNewUser();
            }
        };

        public final Comparator<PlatformData> comparator2 = new Comparator<PlatformData>() {
            public int compare(PlatformData s1, PlatformData s2) {
                //return s2.getNewUser() - s1.getNewUser();
                return BiPlatformManager.getChannelNameById(s1.getPlatformid()).compareTo(BiPlatformManager.getChannelNameById(s2.getPlatformid()));
            }
        };

        public int getNewDevice() {
            return newDevice;
        }

        public void setNewDevice(int newDevice) {
            this.newDevice = newDevice;
        }

        public String getPlatformid() {
            return platformid;
        }

        public void setPlatformid(String platformid) {
            this.platformid = platformid;
        }

        public int getNewUser() {
            return newUser;
        }

        public void setNewUser(int newUser) {
            this.newUser = newUser;
        }

        public int getActiveUser() {
            return activeUser;
        }

        public void setActiveUser(int activeUser) {
            this.activeUser = activeUser;
        }

        public int getPayUser() {
            return payUser;
        }

        public void setPayUser(int payUser) {
            this.payUser = payUser;
        }

        public int getPay() {
            return pay;
        }

        public void setPay(int pay) {
            this.pay = pay;
        }

        public float getPayUserInActiveRadio() {
            return payUserInActiveRadio;
        }

        public void setPayUserInActiveRadio(float payUserInActiveRadio) {
            this.payUserInActiveRadio = payUserInActiveRadio;
        }

        public float getPayARPU() {
            return payARPU;
        }

        public void setPayARPU(float payARPU) {
            this.payARPU = payARPU;
        }

        public float getActiveARPU() {
            return activeARPU;
        }

        public void setActiveARPU(float activeARPU) {
            this.activeARPU = activeARPU;
        }

        public float getThe2LeftRadio() {
            return the2LeftRadio;
        }

        public void setThe2LeftRadio(float the2LeftRadio) {
            this.the2LeftRadio = the2LeftRadio;
        }

        public float getThe3LeftRadio() {
            return the3LeftRadio;
        }

        public void setThe3LeftRadio(float the3LeftRadio) {
            this.the3LeftRadio = the3LeftRadio;
        }

        public float getThe7LeftRadio() {
            return the7LeftRadio;
        }

        public void setThe7LeftRadio(float the7LeftRadio) {
            this.the7LeftRadio = the7LeftRadio;
        }

        public float getThe2MacLeftRadio() {
            return the2MacLeftRadio;
        }

        public void setThe2MacLeftRadio(float the2MacLeftRadio) {
            this.the2MacLeftRadio = the2MacLeftRadio;
        }

        public float getThe3MacLeftRadio() {
            return the3MacLeftRadio;
        }

        public void setThe3MacLeftRadio(float the3MacLeftRadio) {
            this.the3MacLeftRadio = the3MacLeftRadio;
        }

        public float getThe7MacLeftRadio() {
            return the7MacLeftRadio;
        }

        public void setThe7MacLeftRadio(float the7MacLeftRadio) {
            this.the7MacLeftRadio = the7MacLeftRadio;
        }

        public boolean isNotempty() {
            return notempty;
        }

        public void setNotempty(boolean notempty) {
            this.notempty = notempty;
        }

        public int getActivate() {
            return activate;
        }

        public void setActivate(int activate) {
            this.activate = activate;
        }
    }

    // 渠道数据
    public class ChannelData {
        private String channelid; // 渠道ID
        private int activate;
        private int newDevice;  // 新增设备
        private float newActRate;
        private int newUser; // 新增帐号
        private int activeUser; // 活跃帐号
        private int payUser; // 付费帐号
        private int pay; // 付费金额
        private float payUserInActiveRadio; // 活跃付费率
        private float payARPU; // 付费ARPU
        private float activeARPU; // 活跃ARPU
        private float the2LeftRadio; // 次日留存率
        private float the3LeftRadio; // 3日留存率
        private float the7LeftRadio; // 7日留存率

        private float the2MacLeftRadio; // 次日留存率
        private float the3MacLeftRadio; // 3日留存率
        private float the7MacLeftRadio; // 7日留存率

        public final Comparator<ChannelData> comparator = new Comparator<ChannelData>() {
            public int compare(ChannelData s1, ChannelData s2) {
                return s2.getNewUser() - s1.getNewUser();
            }
        };

        public final Comparator<ChannelData> comparator2 = new Comparator<ChannelData>() {
            public int compare(ChannelData s1, ChannelData s2) {
                return BiPlatformManager.getChannelNameById(s1.getChannelid()).compareTo(BiPlatformManager.getChannelNameById(s2.getChannelid()));
            }
        };

        public float getNewActRate() {
            return newActRate;
        }

        public void setNewActRate(float newActRate) {
            this.newActRate = newActRate;
        }

        public int getNewDevice() {
            return newDevice;
        }

        public void setNewDevice(int newDevice) {
            this.newDevice = newDevice;
        }

        public String getChannelid() {
            return channelid;
        }

        public void setChannelid(String channelid) {
            this.channelid = channelid;
        }

        public int getNewUser() {
            return newUser;
        }

        public void setNewUser(int newUser) {
            this.newUser = newUser;
        }

        public int getActiveUser() {
            return activeUser;
        }

        public void setActiveUser(int activeUser) {
            this.activeUser = activeUser;
        }

        public int getPayUser() {
            return payUser;
        }

        public void setPayUser(int payUser) {
            this.payUser = payUser;
        }

        public int getPay() {
            return pay;
        }

        public void setPay(int pay) {
            this.pay = pay;
        }

        public float getPayUserInActiveRadio() {
            return payUserInActiveRadio;
        }

        public void setPayUserInActiveRadio(float payUserInActiveRadio) {
            this.payUserInActiveRadio = payUserInActiveRadio;
        }

        public float getPayARPU() {
            return payARPU;
        }

        public void setPayARPU(float payARPU) {
            this.payARPU = payARPU;
        }

        public float getActiveARPU() {
            return activeARPU;
        }

        public void setActiveARPU(float activeARPU) {
            this.activeARPU = activeARPU;
        }

        public float getThe2LeftRadio() {
            return the2LeftRadio;
        }

        public void setThe2LeftRadio(float the2LeftRadio) {
            this.the2LeftRadio = the2LeftRadio;
        }

        public float getThe3LeftRadio() {
            return the3LeftRadio;
        }

        public void setThe3LeftRadio(float the3LeftRadio) {
            this.the3LeftRadio = the3LeftRadio;
        }

        public float getThe7LeftRadio() {
            return the7LeftRadio;
        }

        public void setThe7LeftRadio(float the7LeftRadio) {
            this.the7LeftRadio = the7LeftRadio;
        }

        public float getThe2MacLeftRadio() {
            return the2MacLeftRadio;
        }

        public void setThe2MacLeftRadio(float the2MacLeftRadio) {
            this.the2MacLeftRadio = the2MacLeftRadio;
        }

        public float getThe3MacLeftRadio() {
            return the3MacLeftRadio;
        }

        public void setThe3MacLeftRadio(float the3MacLeftRadio) {
            this.the3MacLeftRadio = the3MacLeftRadio;
        }

        public float getThe7MacLeftRadio() {
            return the7MacLeftRadio;
        }

        public void setThe7MacLeftRadio(float the7MacLeftRadio) {
            this.the7MacLeftRadio = the7MacLeftRadio;
        }

        public int getActivate() {
            return activate;
        }

        public void setActivate(int activate) {
            this.activate = activate;
        }
    }
}
